package com.lv.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.leyou.common.exception.LyException;
import com.lv.leyou.dto.SpecParamDTO;
import com.lv.leyou.entity.SpecParam;
import com.lv.leyou.mapper.SpecParamMapper;
import com.lv.leyou.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    @Override
    public List<SpecParamDTO> querySpecParam(Long categoryId, Long groupId, Boolean searching) {
        //健壮性判断
        if (categoryId == null && groupId == null) {
            //条件都不能为空
            throw new LyException(400, "查询条件不能为空");
        }
        List<SpecParam> list = query().eq(categoryId != null, "category_id", categoryId)
                .eq(groupId != null, "group_id", groupId)
                .eq(searching != null, "searching", searching).list();
        List<SpecParamDTO> specParamDTOS = SpecParamDTO.convertEntityList(list);

        return specParamDTOS;
    }
    @Transactional
    @Override
    public void addSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        save(specParam);
    }
    @Transactional
    @Override
    //修改规格参数
    public void updateSpecGroup(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        updateById(specParam);
    }
}